package com.xx.pmis.web.actions;

import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.xx.pmis.model.Subject;
import com.xx.pmis.model.util.SubjectTree;
import com.xx.pmis.services.SubjectService;

import com.xx.pmis.web.forms.SubjectForm;
import com.xx.pmis.web.util.DynamicSubjectTree;

/**
 * @class 科目Action类 <br>
 * 
 * @editor 石星星  @Date 2011-9-21 下午03:17:42 <br>
 * @version jdk_1.5.0
 */
public class SubjectAction extends PMISBaseAction {
	private static final Log log = LogFactory.getLog(SubjectAction.class);
	SubjectService subjectService = getPMISServices().getSubjectService();

	// 查询所有科目
	public ActionForward listSubject(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		List<Subject> subjectList = null;
		try {
			subjectList = subjectService.getAllSubjectByCondition(" where subParentID = 0 order by subCode asc");
		} catch (Exception e) {
			log.info("【财务管理/科目】查询科目异常！" + e.toString());
		}
		DynamicSubjectTree dynamicSubjectTree = new DynamicSubjectTree();
		List<SubjectTree> subjectTreeList = dynamicSubjectTree.MadeSubjectTree();
		try {
			int count = subjectService.getCountOfSubject("");
			// 科目数，查询系统中共有多少条科目
			request.setAttribute("count", String.valueOf(count));
		} catch (Exception e) {
			log.info("【财务管理/科目】查询科目数目异常！" + e.toString());
		}
		// 科目树
		request.getSession(true).setAttribute("subjectTreeList", subjectTreeList);
		request.setAttribute("subjectList", subjectList);
		request.setAttribute("position", "财务管理/科目");
		return mapping.findForward("listSubject");
	}
	
	// 查询所有下级科目
	public ActionForward listNextSubject(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String subjectID = request.getParameter("subjectID");
		List<Subject> subjectList = null;
		try {
			subjectList = subjectService.getAllSubjectByCondition(" where subParentID = '" + subjectID + "' order by subCode asc");
		} catch (Exception e) {
			log.info("【财务管理/科目】查询科目异常！" + e.toString());
		}
		DynamicSubjectTree dynamicSubjectTree = new DynamicSubjectTree();
		List<SubjectTree> subjectTreeList = dynamicSubjectTree.MadeSubjectTree();
		try {
			int count = subjectService.getCountOfSubject("");
			// 科目数，查询系统中共有多少条科目
			request.setAttribute("count", String.valueOf(count));
		} catch (Exception e) {
			log.info("【财务管理/科目】查询科目数目异常！" + e.toString());
		}
		// 科目树
		request.getSession(true).setAttribute("subjectTreeList", subjectTreeList);
		Subject subject = new Subject();
		try {
			subject = subjectService.getSubjectBySubjectID(subjectID);
		} catch (Exception e) {
			log.info("【财务管理/科目】跟进科目ID查询该科目异常！" + e.toString());
		}
		request.setAttribute("subjectList", subjectList);
		request.setAttribute("subjectID", subjectID);
		request.setAttribute("subjectName", subject.getSubName());
		request.setAttribute("position", "财务管理/科目");
		return mapping.findForward("listSubject");
	}
	
	// 新增
	public ActionForward addSubject(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String subjectID = request.getParameter("subjectID");
		request.setAttribute("subjectID", subjectID);
		request.setAttribute("position", "科目/新增");
		return mapping.findForward("addSubject");
	}
	
	// 新增下级科目
	public ActionForward addNextSubject(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String subjectID = request.getParameter("subjectID");
		Subject subject = null;
		try {
			subject = subjectService.getSubjectBySubjectID(subjectID);
		} catch (Exception e) {
			log.info("【财务管理/科目/二级科目/新增】根据科目ID查询该科目异常！" + e.toString());
		}
		request.setAttribute("subject", subject);
		request.setAttribute("position", "二级科目/新增");
		return mapping.findForward("addNextSubject");
	}
	
	// 新增，处理方法
	public ActionForward addSubject_Do(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		SubjectForm f = (SubjectForm) form;
		String subName = f.getSubName(); // 科目名称
		String subCode = f.getSubCode(); // 科目代码
		String subDesc = f.getSubDesc(); // 描述
		String subParentID = f.getSubParentID();

		Subject subject = new Subject();
		subject.setSubID(subCode);
		
		subject.setSubName(subName);
		subject.setSubCode(subCode);
		subject.setSubState("1"); // 新增的科目都处于停用状态，需要管理员或者上级领导审批后才能使用
		subject.setSubDesc(subDesc);
		subject.setSubParentID(subParentID);
		
		try {
			subjectService.saveOrUpdateSubject(subject);
		} catch (Exception e) {
			log.info("【财务管理/科目/新增】保存科目异常！" + e.toString());
		}
		if(subParentID.equals("0")) {
			listSubject(mapping, form, request, response);
			return mapping.findForward("listSubject");
		} else {
			listSubject(mapping, form, request, response);
			return mapping.findForward("listNextSubject");
		}
	

	}
	
	// 编辑
	public ActionForward editSubject(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String subID = request.getParameter("subID");

		Subject subject = new Subject();
		try {
			subject = subjectService.getSubjectBySubjectID(subID);
		} catch (Exception e) {
			log.info("【财务管理/科目】跟进科目ID查询该科目异常！" + e.toString());
		}
		request.setAttribute("position", "科目/编辑");
		request.setAttribute("subject", subject);
		return mapping.findForward("editSubject");
	}

	// 编辑，处理方法
	public ActionForward editSubject_Do(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		SubjectForm f = (SubjectForm) form;
		String subID = f.getSubID();
		String subName = f.getSubName(); // 科目名称
		String subCode = f.getSubCode(); // 科目代码
		String subDesc = f.getSubDesc(); // 描述

		Subject subject = null;
		try {
			subject = subjectService.getSubjectBySubjectID(subID);
		} catch (Exception e) {
			log.info("【财务管理/科目】根据科目ID查询该科目异常！" + e.toString());
		}
		subject.setSubName(subName);
		subject.setSubCode(subCode);
		subject.setSubDesc(subDesc);
		try {
			subjectService.saveOrUpdateSubject(subject);
		} catch (Exception e) {
			log.info("【财务管理/科目/新增】保存科目异常！" + e.toString());
		}
		
		listSubject(mapping, form, request, response);
		return mapping.findForward("listSubject");
	}

	// 删除
	public ActionForward delSubject(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		Subject subject = null;
		SubjectForm f = (SubjectForm) form;
		String[] ids = f.getIds();
		for (int i = 0; i < ids.length; i++) {
			try {
				subject = subjectService.getSubjectBySubjectID(ids[i]);
			} catch (Exception e) {
				log.info("【财务管理/科目】根据科目ID查询该科目异常！" + e.toString());
			}
			try {
				subjectService.deleteSubject(subject);
			} catch (Exception e) {
				log.info("【财务管理/科目/删除】保存科目异常！" + e.toString());
			}
		}

		listSubject(mapping, form, request, response);
		return mapping.findForward("listSubject");
	}
	
    // 停用
	public ActionForward stopSubject(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String subjectID = request.getParameter("subjectID");
		// 财务科目
		Subject subject = null;
		try {
			subject = subjectService.getSubjectBySubjectID(subjectID);
		} catch (Exception e) {
			log.info("【财务管理/科目】根据科目ID查询该科目异常！" + e.toString());
		}

		// 如果该科目下存在二级科目且二级科目处在启用状态，不能停用
		List<Subject> subjectList = null;
		try {
			subjectList = subjectService.getAllSubjectByCondition(" where subParentID = '" + subjectID  + "'");
		} catch (Exception e) {
			log.info("【财务管理/科目】根据科目ID查询该科目下的二级科目异常！" + e.toString());
		}
		if(subjectList.size()>0) {// 不存在二级科目直接退出循环
			Iterator<?> it = subjectList.iterator();
			int i = 0;
			while(it.hasNext()) {
				Subject subject2 = (Subject) it.next();
				if(subject2.getSubState().equals("0")) {
					i = i + 1;
				}
			}
			if(i>0) {
				request.setAttribute("error", "该科目下存在正在使用的二级科目，暂时不能停用！");
				listSubject(mapping, form, request, response);	
				return mapping.findForward("listSubject");	
			} else {
				subject.setSubState("1");
			}
			
		} else {
			subject.setSubState("1");
		}
		try {
			subjectService.saveOrUpdateSubject(subject);
		} catch (Exception e) {
			log.info("【财务管理/科目/停用】保存科目异常！" + e.toString());
		}

		listSubject(mapping, form, request, response);	
		return mapping.findForward("listSubject");
	}
    // 启用
	public ActionForward startSubject(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String subjectID = request.getParameter("subjectID");
		Subject subject = null;
		try {
			subject = subjectService.getSubjectBySubjectID(subjectID);
		} catch (Exception e) {
			log.info("【财务管理/科目】根据科目ID查询该科目异常！" + e.toString());
		}
		subject.setSubState("0");
		try {
			subjectService.saveOrUpdateSubject(subject);
		} catch (Exception e) {
			log.info("【财务管理/科目/启用】保存科目异常！" + e.toString());
		}
		listSubject(mapping, form, request, response);
		
		return mapping.findForward("listSubject");
	}
}